//
//  MyNoteViewController.m
//  MansApp
//
//  Created by lanou3g on 15/7/13.
//  Copyright (c) 2015年 ACH. All rights reserved.
//

#import "MyNoteViewController.h"
#import "MyCollectionViewCell.h"
#import "RightViewController.h"
#import "UIViewController+MMDrawerController.h"
#import "MyNoteBookViewController.h"
#import "InfoTextAttachment.h"
#import "MyNoteSingleton.h"

@interface MyNoteViewController ()<UICollectionViewDelegate,UICollectionViewDataSource,UICollectionViewDelegateFlowLayout >
@property(nonatomic,strong)UICollectionView *collectionView;
@property(nonatomic,strong)NSMutableArray *dataArray;
@property(nonatomic,assign)BOOL isArrayNone;

@end

@implementation MyNoteViewController

- (void)viewDidLoad {
    [super viewDidLoad];
      [[ACHdb sharedInstance] createTable];
      [self setDataArrayWithCollection];
    GroundGlass *glass = [[GroundGlass alloc]initWithFrame:self.view.frame style:UIBlurEffectStyleLight];
    glass.image = [UIImage imageNamed:@"181034rin3dycr6k8y6hvg"];
    [self.view addSubview:glass];
    self.navigationController.navigationBar.translucent = NO;
    UICollectionViewFlowLayout *layou = [[UICollectionViewFlowLayout alloc]init];
    layou.scrollDirection = UICollectionViewScrollDirectionHorizontal;
    self.collectionView = [[UICollectionView alloc]initWithFrame:CGRectMake(0, 0, kWidth, kP6Height(300)) collectionViewLayout:layou];
    self.collectionView.delegate = self;
    self.collectionView.dataSource = self;
    self.collectionView.backgroundColor =  [UIColor clearColor];
    [self.view addSubview: self.collectionView];
    
    UIButton *writeNoteButton = [UIButton buttonWithType:UIButtonTypeSystem];
    writeNoteButton.frame = CGRectMake(0, 0, kP6Height(80),kP6Height(80));
    writeNoteButton.center = CGPointMake(self.view.center.x,kP6Height(350));
    [writeNoteButton setBackgroundImage:[UIImage imageNamed:@"135227"] forState:UIControlStateNormal];
    [writeNoteButton addTarget:self action:@selector(writeNoteButtonAction:) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:writeNoteButton];
    
    UIBarButtonItem *rightButton = [[UIBarButtonItem alloc]initWithTitle:@"more" style:UIBarButtonItemStyleDone target:self action:@selector(rightBarButtonAction:)];
    
    
    self.navigationItem.rightBarButtonItem = rightButton;
    
    // 创建表格
   
    
    
    [MyNoteSingleton shareInstance].reloadBlcok = ^(){
        
        [self setDataArrayWithCollection];
        [self.collectionView reloadData];
        
    };
    
    
    
    
    
}

-(void)writeNoteButtonAction:(UIButton *)sender{
    
    MyNoteBookViewController *noteBook = [[MyNoteBookViewController alloc]init];
    [self presentViewController:noteBook animated:YES completion:nil];
    
    
    
    
}

- (void)rightBarButtonAction:(UIBarButtonItem *)sender{
    
    [self.mm_drawerController  toggleDrawerSide:MMDrawerSideRight animated:YES completion:nil];
    
}

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{
    
    
    return  [self.dataArray.firstObject count];
    
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
    static NSString *cellIdentifier = @"item";
    [collectionView registerClass:[MyCollectionViewCell class] forCellWithReuseIdentifier:cellIdentifier];
    MyCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:cellIdentifier forIndexPath:indexPath];
    if (_isArrayNone) {
        
        cell.myLabel.text = [self labelTextWithIndex:indexPath.row];
    }
    
    
    
    return cell;
}

- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath{
    
    MyNoteBookViewController *noteBook = [[MyNoteBookViewController alloc]init];
    
    
    noteBook.diaryModel = [[ACHdb sharedInstance] showOneWithDate:self.dataArray.firstObject[indexPath.row]];
    
    NSArray *array = [noteBook.diaryModel.tape componentsSeparatedByString:@"/"];
    
    __block NSArray *arr =  array;
    __block int i = 0;
    __block int j = 0;
    [noteBook.diaryModel.diary enumerateAttribute:NSAttachmentAttributeName inRange:NSMakeRange(0, noteBook.diaryModel.diary.length) options:NSAttributedStringEnumerationReverse usingBlock:^(NSTextAttachment *value, NSRange range, BOOL *stop) {
        
        if (value && [value isKindOfClass:[InfoTextAttachment class]]) {
            
            ((InfoTextAttachment*)value).infoString = arr[i];
            i++;
            
            value.bounds = CGRectMake(0, 0,kP6width(300), kP6Height(30));
            
        }
        else if(value && [value isKindOfClass:[NSTextAttachment class]]){
            
            NSData *data = noteBook.diaryModel.picArray[j];
            value.image =  [UIImage imageWithData:data scale:1.0 ];
            j++;
            CGSize size = value.image.size;
            value.bounds = CGRectMake(0, 0, kP6width(80), kP6Height(80) * size.height/ size.width );
            
        }
    }];
    
    
    NSLog(@"%@",array);
    
    
    [self presentViewController:noteBook animated:YES completion:nil];
    
    
    
    
}

- (NSString *)labelTextWithIndex:(NSInteger)index{
    
    if (self.dataArray.lastObject[index] == nil || [self.dataArray.lastObject[index] length] == 0) {
        
        return self.dataArray.firstObject[index];
    }
    
    return self.dataArray.lastObject[index];
}



- (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout minimumInteritemSpacingForSectionAtIndex:(NSInteger)section{
    
    return kP6Height(20);
}
- (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout minimumLineSpacingForSectionAtIndex:(NSInteger)section{
    return kP6Height(20);
}

- (UIEdgeInsets)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout insetForSectionAtIndex:(NSInteger)section{
    
    return UIEdgeInsetsMake(kP6Height(20),kP6width(20), kP6Height(10),kP6width(20));
}

- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath{
    return CGSizeMake(kP6width(90), kP6Height(120));
}





- (void)setDataArrayWithCollection{
    NSMutableArray *arr = [[ACHdb sharedInstance] showTheTableAll];
    
//    NSLog(@"%@",arr.firstObject);
    if (arr != nil && [arr.firstObject count] != 0) {
        self.dataArray = nil;
        self.dataArray = arr;
        self.isArrayNone = YES;
     
    }
    else{
        self.dataArray = nil;
        self.dataArray = [NSMutableArray array];
        self.isArrayNone = NO;
    }
    
    
}

- (void)viewDidAppear:(BOOL)animated{
    
    [super viewDidAppear:animated];
    
    [self setDataArrayWithCollection];
    [self.collectionView reloadData];
    
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
 #pragma mark - Navigation
 
 // In a storyboard-based application, you will often want to do a little preparation before navigation
 - (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
 // Get the new view controller using [segue destinationViewController].
 // Pass the selected object to the new view controller.
 }
 */

@end
